"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】执行触发table_rewrite事件触发器，事件在表重写之前触发
Create At   :   2024/07/20
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0070()
        RETURNS event_trigger LANGUAGE plpgsql AS $$
        BEGIN
        RAISE NOTICE 'rewriting table % for reason %',
        pg_event_trigger_table_rewrite_oid()::regclass,
        pg_event_trigger_table_rewrite_reason();
        END;
        $$;
        2.创建表
        create table t_eventtrigger_0070(id int, col varchar(10));
    测试步骤：
        1.创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0070 ON table_rewrite EXECUTE PROCEDURE f_eventtrigger_0070();
        2.修改表触发重写
        alter table t_eventtrigger_0070 alter col type int using (col::int);
Expect      :
        1.创建事件触发器成功
        2.修改表触发执行函数f_eventtrigger_0070()
        NOTICE：rewriting table t_eventtrigger_0070 for reason 4;修改表成功
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0070;")
        self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0070;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0070();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0070() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'rewriting table % for reason %', " \
            f" pg_event_trigger_table_rewrite_oid()::regclass," \
            f" pg_event_trigger_table_rewrite_reason();" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(f"create table t_eventtrigger_0070(id int, col varchar(10));")
        self.logger.info(msg)
        self.assertIn("CREATE TABLE", msg)

    def test_Index_file_damaged(self):
        self.logger.info("--step1:创建事件触发器")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0070 ON table_rewrite EXECUTE PROCEDURE f_eventtrigger_0070();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)
        self.logger.info("--step2:修改表触发重写")
        msg = self.comsh.execut_db_sql("alter table t_eventtrigger_0070 alter col type int using(col::int);")
        self.logger.info(msg)
        self.assertIn(f'NOTICE:  rewriting table t_eventtrigger_0070 for reason 4' and f"ALTER TABLE", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0070;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0070();")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0070;")
        self.logger.info(msg)
        self.assertEqual('DROP TABLE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
